package controler.p3_fitnessapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import listAdapterHistory.p3_fitnessapp.HistoryDetails;
import model.p3_fitnessapp.Exercise;
import model.p3_fitnessapp.History;
import model.p3_fitnessapp.MySQLiteHelper;
import model.p3_fitnessapp.Timemanager;
import model.p3_fitnessapp.TrainingPlan;
import unteractivities.p3_fitnessapp.UActHistoryDetails;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

/**
 * Hauptklasse, diese Klasse wird beim Start des Programms aufgerufen
 * 
 * @author maxkuhlmay
 * 
 * @see ActEditExercise
 * @see ActHistory
 * @see ActSettings
 * @see ActNutrients
 * @see ActNewTraining
 * @see ActTrainingplan
 * 
 */

public class MainActivity extends Activity {

	/*
	 * ==========================================================================
	 * Globale Variablen
	 * ========================================================
	 */

	public static String PACKAGE_NAME;
	public String DB_PATH = "";

	/*
	 * ==========================================================================
	 * Android Methoden
	 * ==========================================================
	 */

	/**
	 * 
	 * @author maxkuhlmay edited by mauricegregor
	 */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_main);

		Log.i(null, "onCreate(MainActivity)");

		PACKAGE_NAME = getApplicationContext().getPackageName();
		// TODO Warning

		DB_PATH = "/data/data/" + PACKAGE_NAME + "/databases/";

		migrateDatabase();

		// Aufruf Testmethode

//		 testDBMethods();
		 
		 
	}

	/**
	 * @author maxkuhlmay created by Android
	 * @param menu
	 */

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		Log.i(null, "onCreateOptionsMenu(MainActivity)");

		return true;

	}

	@Override
	protected void onResume() {
		super.onResume();

		SharedPreferences preferences = getSharedPreferences(getPackageName()
				+ "_preferences", MODE_PRIVATE);

		String benutzername = preferences.getString("benutzername", "Fremd");

		TextView tv = (TextView) findViewById(R.id.txt_welcome);

		tv.setText("Hallo, " + benutzername);

	}

	/*
	 * ==========================================================================
	 * Benutzer Interaktion (Button klicken ... )
	 * ================================
	 */

	/**
	 * Methode wird ausgefuehrt, sobald ein Button auf der MainActivity
	 * gedrueckt wird
	 * 
	 * @author maxkuhlmay
	 * @param view
	 * @return ActNewTraining, ActTrainingplan, ActNutrients, ActEditExercise,
	 *         ActSettings, ActHistory
	 */

	public void onButtonClick(View view) {
		switch (view.getId()) {
		case R.id.btn_new_training:
			startActivity(new Intent(this, ActNewTraining.class));
			break;
		case R.id.btn_edit_trainingsplan:
			startActivity(new Intent(this, ActTrainingplan.class));
			break;
		case R.id.btn_nutrients_calc:
			startActivity(new Intent(this, ActNutrients.class));
			break;
		case R.id.btn_edit_exercises:

			Log.i(null, "Button clicked");
			startActivity(new Intent(this, ActEditExercise.class));
			Log.i(null, "startActivity(ActEditExercise)");
			break;
		case R.id.btn_action_settings:
			startActivity(new Intent(this, ActSettings.class));
			break;
		case R.id.btn_history:
//			startActivity(new Intent(this, ActHistory.class));
//			startActivity(new Intent(this, UActHistoryDetails.class));
			startActivity(new Intent(this, listAdapterHistory.p3_fitnessapp.HistoryDetails.class));
			break;

		default:
			break;
		}
	}

	/*
	 * ==========================================================================
	 * Eigene Methoden
	 * ==========================================================
	 */

	/**
	 * 
	 * @author mauricegregor
	 */
	private void migrateDatabase() {

		MySQLiteHelper myDbHelper = new MySQLiteHelper(getApplicationContext(),
				DB_PATH);

		try {
			myDbHelper.createDataBase();
		} catch (IOException ioe) {

			writeLog(null, "Unable to create database", "d");
			throw new Error("Unable to create database");
		}

		try {
			myDbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
	}

	/**
	 * 
	 * @author mauricegregor
	 */
	private void testDBMethods() {

		MySQLiteHelper myDbHelper = new MySQLiteHelper(getApplicationContext(),
				DB_PATH);

		// Verbindungstest SQLite Datenbank

		writeLog("TEST:DB:addExercise", "bevor TEST! insert Exercise", "i");

		Exercise test_ex = new Exercise();
		test_ex.setTitel("Test_Ex");
		test_ex.setMusclegroup("Gesäß");
		myDbHelper.addExercise(test_ex);

		writeLog("TEST:DB:getALLTraininsplans",
				"bevor TEST getALLTrainingplans!", "i");

		myDbHelper.getAllTrainingplans();

		writeLog("TEST:DB:getExerciseByTrainingplanID",
				"bevor TEST getExerciseByTrainingplanID!", "i");

		int id_wp = 1;

		myDbHelper.getExerciseByTrainingplanID(id_wp);

		writeLog("TEST:DB:addTrainingplanWithExercises",
				"bevor TEST addTrainingplanWithExercises!", "i");

		TrainingPlan tp = new TrainingPlan();
		tp.setName("Test_TP");

		Exercise ex = new Exercise();
		ex.setTitel("Test_EX");

		ArrayList<Exercise> a_ex = new ArrayList<Exercise>();
		a_ex.add(ex);
		a_ex.add(ex);

		myDbHelper.addTrainingplanWithExercises(tp, a_ex);

		myDbHelper.getAllTrainingplans();

		writeLog("TEST:DB:getAllMuscleGroups",
				"bevor TEST getAllMuscleGroups!", "i");

		myDbHelper.getAllMusclegroups();

		writeLog("TEST:DB:getMuscleGroupbyExerciseID",
				"bevor TEST getMuscleGroupByExerciseID!", "i");

		myDbHelper.getMuscleGroupByExerciseID(53);

		writeLog("TEST:DB:getExerciseByMuscleGroupID",
				"bevor TEST getExerciseByMuscleGroupID!", "i");

		myDbHelper.getExerciseByMusclegroupID(5);

		writeLog("TEST:DB:getMuscleGroupByTrainingplanID",
				"bevor TEST getMuscleGroupByTrainingplanID!", "i");

		myDbHelper.getMuscleGroupByTrainingplanID(1);
		myDbHelper.getMuscleGroupByTrainingplanID(2);
	
		writeLog("TEST:DB:getExerciseByMuscleGroupIDandTrainingplanID", "bevor TEST getExerciseByMuscleGroupIDandTrainingplanID!", "i");
		
		myDbHelper.getExerciseByMuscleGroupIDandTrainingplanID(5, 1);

		writeLog("TEST:DB:Timemanager.dateToString", "bevor TEST dateToString!", "i");
		
		Timemanager time = new Timemanager();
				
		System.out.println("CalendarToString: " + time.calendarToString(time.createCurrentCalendar()));
		
		writeLog("TEST:DB:addHistory", "bevor TEST History!", "i");

		History h = new History();
		h.setDate("heute");
		h.setId_exercise(53);
		h.setSet(1);
		h.setKilograms(7);
		h.setRepetition(23);

		myDbHelper.addHistory(h);
		
		writeLog("TEST:DB:getAllHistory", "bevor TEST getAllHistory!", "i");
		
		myDbHelper.getAllHistorys();
	}

	/*
	 * ==========================================================================
	 * Debug Methoden
	 * ============================================================
	 */

	/**
	 * Methode zur Ausgabe der Log Nachrichte
	 * 
	 * @param tag
	 * @param message
	 * @param type
	 *            Logtype
	 */
	private void writeLog(String tag, String message, String type) {

		if (type.equals("d")) {
			Log.d(tag, message);
		}
		if (type.equals("i")) {
			Log.i(tag, message);
		}

	}

	/*
	 * ==========================================================================
	 * Getter und Setter
	 * ========================================================
	 */

}